package com.qlslylq.ad.sdk.application;

import android.app.Application;
import android.content.Context;

import com.qlslylq.ad.sdk.BuildConfig;
import com.qlslylq.ad.sdk.R;
import com.qlslylq.ad.sdk.config.CsjMgrHolder;
import com.qlslylq.ad.sdk.config.KsMgrHolder;
import com.qlslylq.ad.sdk.config.MintegralMgrHolder;
import com.qlslylq.ad.sdk.config.SigmobMgrHolder;
import com.qlslylq.ad.sdk.config.YlhMgrHolder;
import com.qlslylq.ad.sdk.constant.AppConst;
import com.qlslylq.ad.sdk.core.mgr.MediaConfigMgr;
import com.qlslylq.ad.sdk.core.mgr.PolicyCursorMgr;
import com.qlslylq.ad.sdk.enums.AdEventSign;
import com.qlslylq.ad.sdk.enums.AdPlatformEnum;
import com.qlslylq.ad.sdk.interfaces.InitCallback;
import com.qlslylq.ad.sdk.method.AppMethod;
import com.qlslylq.ad.sdk.model.req.AppBehaviorAddDTO;
import com.qlslylq.ad.sdk.model.rsp.MediaWebVO;
import com.qlslylq.ad.sdk.net.ResponseObject;
import com.qlslylq.ad.sdk.net.api.ApiServiceImpl;
import com.qlslylq.ad.sdk.net.netdector.NetworkUtils;
import com.qlslylq.ad.sdk.rx.subscribers.OnNextOnError;
import com.qlslylq.ad.sdk.util.GsonUtils;
import com.qlslylq.ad.sdk.util.Log;

import static com.qlslylq.ad.sdk.enums.ErrorEnum.ERR_INIT_DY_SDK;

/**
 * SDK入口<br/>
 */
public class AdMergeSDK {

    private static Application app;

    //SDK初始化配置信息
    private static AdMergeSDKConfig config;

    //媒体配置信息
    private static MediaWebVO mediaWebVO;

    /**
     * SDK初始化<br/>
     */
    public static void init(Application app, AdMergeSDKConfig config, InitCallback callback) {
        if (config == null) {
            throw new IllegalArgumentException("参数不能为null");
        }
        if (!AppConst.openSecretLog) {
            if (config.isTest()) {
                throw new IllegalArgumentException("测试环境已不对外开放，请使用正式环境");
            }
        }

        AdMergeSDK.app = app;
        AdMergeSDK.config = config;
        PolicyCursorMgr.getInstance().resetAllCursor();
        initSDK_Stetho();
        String deviceId = AppMethod.getDeviceId();
        Log.dp(String.format("设备唯一标识：%s", deviceId));

        MediaWebVO item = MediaConfigMgr.getInstance().getMediaConfig();
        if (item == null) {
            ApiServiceImpl.getMediaConfigList(config.getAppId(), new OnNextOnError<ResponseObject<MediaWebVO>>() {
                public void onNext(ResponseObject<MediaWebVO> rsp) {
                    AdMergeSDK.mediaWebVO = rsp.getData();
                    MediaConfigMgr.getInstance().setMediaConfig(AdMergeSDK.mediaWebVO);
                    initSDK(callback);
                    if (callback != null) {
                        callback.success();
                    }
                    ApiServiceImpl.addTrack(AppBehaviorAddDTO.build(AdEventSign.APP_OPEN));
                }

                public void onError(ResponseObject rsp) {
                    Log.ep(String.format("获取配置信息失败：%s", GsonUtils.gsonString(rsp)));
                    if (callback != null) {
                        if (NetworkUtils.isConnected(app)) {
                            callback.fail(AdPlatformEnum.PLATFORM_DY, ERR_INIT_DY_SDK.getId(), ERR_INIT_DY_SDK.getName());
                        } else {
                            callback.fail(AdPlatformEnum.PLATFORM_DY, rsp.getCode(), rsp.getMessage());
                        }
                    }
                }
            });
        } else {
            AdMergeSDK.mediaWebVO = item;
            initSDK(callback);
            if (callback != null) {
                callback.success();
            }
            ApiServiceImpl.addTrack(AppBehaviorAddDTO.build(AdEventSign.APP_OPEN));
        }
    }

    public static Application getApp() {
        return app;
    }

    /**
     * 初始化第三方SDK<br/>
     */
    private static void initSDK(InitCallback callback) {
        CsjMgrHolder.init(app, callback);
        YlhMgrHolder.init(app, callback);
        KsMgrHolder.init(app, callback);
        SigmobMgrHolder.init(app, callback);
        MintegralMgrHolder.init(app, callback);
    }

    /**
     * 初始化facebook网络调试<br/>
     */
    private static void initSDK_Stetho() {
        if (BuildConfig.DEBUG) {
//            Stetho.initialize(Stetho.newInitializerBuilder(app)
//                    .enableDumpapp(Stetho.defaultDumperPluginsProvider(app))
//                    .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(app))
//                    .build());
        }
    }

    public static AdMergeSDKConfig getConfig() {
        return config;
    }

    public static MediaWebVO getMediaWebVO() {
        return mediaWebVO;
    }

    public static void setMediaWebVO(MediaWebVO mediaWebVO) {
        AdMergeSDK.mediaWebVO = mediaWebVO;
    }

    /**
     * 获取SDK名称<br/>
     */
    public static String getSDKName(Context context) {
        return context.getString(R.string.ad_merge_sdk_name);
    }

    /**
     * 获取版本名称<br/>
     */
    public static String getVersionName() {
        return AppConst.SDK_API_VERSION;
    }

    /**
     * 获取版本号<br/>
     */
    public static int getVersionCode() {
        return AppConst.SDK_API_VERSION_CODE;
    }
}
